package com.lijiajian.controller;

import com.github.pagehelper.PageException;
import com.lijiajian.Service.UserService;
import com.lijiajian.annoation.RequiredPermission;
import com.lijiajian.base.BaseController;
import com.lijiajian.base.ResultInfo;
import com.lijiajian.exceptions.ParamsException;
import com.lijiajian.model.UserModel;
import com.lijiajian.pojo.SaleChance;
import com.lijiajian.pojo.User;
import com.lijiajian.query.UserQuery;
import com.lijiajian.utils.LoginUserUtil;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController extends BaseController {
    @Resource
    private UserService userService;
    @PostMapping("login")
    @ResponseBody
    public ResultInfo userLogin(String userName,String userPwd){
        ResultInfo resultInfo=new ResultInfo();

        UserModel userModel = userService.userLogin(userName, userPwd);
        resultInfo.setResult(userModel);


        return resultInfo;

    }

    /**
     * 跳转到修改密码页面
     * @return
     */
    @GetMapping("updatePWDPage")
    public String updatePwdPage(){

        return "user/password";
    }

    /**
     * 修改密码
     * @param request
     * @param oldPwd
     * @param newPwd
     * @param repeatPwd
     * @return
     */
    @PostMapping("updatePWD")
    @ResponseBody
    public ResultInfo updatePwd(HttpServletRequest request,String oldPwd,String newPwd,String repeatPwd){
        ResultInfo resultInfo=new ResultInfo();

        int userId = LoginUserUtil.releaseUserIdFromCookie(request);
        userService.updatePassword(userId,oldPwd,newPwd,repeatPwd);



        return resultInfo;
    }
    /**
     * 得到所有销售人员
     * @return
     */
    @GetMapping("getSales")
    @ResponseBody
    public List<User> getAllSales(){
        return userService.getAllSales();
    }

    @RequiredPermission(code="601002")
    @RequestMapping("List")
    @ResponseBody
    public Map<String,Object> getUserList(UserQuery userQuery){
        return userService.queryByParamsForTable(userQuery);
    }

    @RequiredPermission(code="6010")
    @RequestMapping("index")
    public String index(){
        return "user/user";
    }

    /**
     * 前往添加或修改页面
     * @return
     */
    @GetMapping("toAddUserPage")
    public String toAddUserPage(Integer id,HttpServletRequest request){
        if(id!=null){
            //更新操作
            request.setAttribute("userModel",userService.selectByPrimaryKey(id));
        }
        return "user/addAndUpdateUser";
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @RequiredPermission(code="601001")
    @PostMapping("add")
    @ResponseBody
    public ResultInfo addUser(User user){

        userService.addUser(user);

        return success("添加成功");
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @RequiredPermission(code="601003")
    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateUser(User user){

        userService.updateUser(user);

        return success("修改成功");
    }

    /**
     * 删除用户
     * @param
     * @return
     */
    @RequiredPermission(code="601004")
    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteUser(Integer[] ids ){

        userService.deleteUser(ids);

        return success("删除成功");
    }

}
